<template>
  <div class="content">
    <!-- 遮罩层 -->
    <van-overlay :show="show" :duration="0.15">
      <div class="wrapper">
        <div class="block">
          <van-icon name="cross" class="close" @click="show=false"/>
          <svg class="icon ic-left" aria-hidden="true">
            <use xlink:href="#icon-guozhi"></use>
          </svg>
          <svg class="icon ic-right" aria-hidden="true">
            <use xlink:href="#icon-xigua"></use>
          </svg>
          <h3>聊天室</h3>
          <input
            type="text"
            @blur="goBottom"
            @keyup.enter="join"
            ref="login"
            v-model="name"
            maxlength="8"
            placeholder="请输入2至8位昵称~"
          >
          <van-button class="start" @click="join" color="skyblue" size="small">进入</van-button>
        </div>
      </div>
    </van-overlay>
    <!-- 头部标题 -->
    <div class="title">
      <div class="titleBox">
        <van-icon class="arrow-left" name="arrow-left"/>
        <p>众志成城 | 共同战疫 🇨🇳</p>
        <van-icon class="ellipsis" name="ellipsis"/>
      </div>
      <div class="messageBox">
        <van-notice-bar @close='closeNotice' mode="closeable" left-icon="volume-o">富强、民主、文明、和谐、自由、平等、公正、法治、爱国、敬业、诚信、友善。</van-notice-bar>
      </div>
    </div>
    <!-- 消息盒子 -->
    <div id="box" @click="closeEmoji">
      <div v-for="(item,index) in datas" :key="index" v-html="item"></div>
      <!-- <div class="main-left clearfix">
            <img class="img-left" src="../assets/img/01.png" alt="">
            <div class="qipao-left">
                <p class="nicheng-left ">我是昵称</p>
                <p class="content">
                    这上面的夜的天空，奇怪而高，我生平没有见过这样奇怪而高的天空。他仿佛要离开人间而去，使人们仰面不再看见。然而现在却非常之蓝，闪闪地〖目夹〗着几十个星星的眼，冷眼。他的口角上现出微笑，似乎自以为大有深意，而将繁霜洒在我的园里的野花上。
                </p>
                <svg class="icon i-left" aria-hidden="true">
                    <use xlink:href="#icon-fengzheng1"></use>
                </svg>
                <svg class="icon i-left2" aria-hidden="true">
                    <use xlink:href="#icon-icon-test1"></use>
                </svg>
                <span class="p-left"></span>
            </div>
        </div>

        <div class="main-right  clearfix"><img class="img-right" src="../assets/img/02.png" alt="">
            <div class="qipao-right">
                <p class="nicheng-right ">我是昵称</p>
                <p class="content">我是一个段落我是一个段落</p>
                <svg class="icon i-right" aria-hidden="true">
                    <use xlink:href="#icon-qishuiMBE"></use>
                </svg>
                <svg class="icon i-right2" aria-hidden="true">
                    <use xlink:href="#icon-xigua"></use>
                </svg>
                <span class="p-right"></span>
            </div>
      </div>-->
    </div>
    <div class="sendBox">
      <div class="inputbox">
        <input
          @blur="goBottom"
          @keyup.enter="enterSendMessage"
          ref="myinput"
          type="text"
          placeholder="打个招呼吧~"
          v-model="text"
          maxlength="50"
          class="sendInp"
        >
        <van-icon @click="showEmoji" class="biaoqing" name="smile-o"/>
        <van-button @click="sendMessage" type="primary" size="small">发送</van-button>
      </div>
      <div v-show="emojiBox" class="emojiBox">
        <span @click="getEmoji" v-for="(item,index) in emojiArr" :key="index" v-html="item"></span>
      </div>
    </div>
  </div>
</template>
           
<script>
import { Toast } from "vant";
import { Notify } from "vant";
import { emojiArr } from "@/assets/js/emoji.js";
export default {
  watch: {
    emojiBox() {
      var h = document.querySelector(".content");
      if (this.emojiBox) {
        h.style.paddingBottom = "240px";
      } else {
        h.style.paddingBottom = "60px";
      }
      this.autoGoBottom();
    }
    //  boxScoll(){
    //   // var box = document.querySelector('#box').scrollTop
    //   console.log(boxScoll);

    // }
  },
  computed: {},
  created() {},
  mounted() {
    // var box = document.querySelector('#box')
    // box.addEventListener('touchmove',()=>{
    //   // console.log('touchstart触发了');
    //   console.log(box.scrollTop);
    // })
    // document.querySelector('.content').addEventListener('click',()=>{
    //   this.$refs.login.focus()
    // })
  },
  data() {
    return {
      datas: [], //用户发送的消息总集合
      text: "", //用户输入框中的内容 限制50字以内
      name: "", //用户昵称
      show: true, //遮罩层显示与隐藏
      isLogin: false, //用户是否登录
      emojiArr: emojiArr, //emoji表情数据包
      emojiBox: false //表情包选择框的显示与隐藏,
    };
  },
  sockets: {
    connection: function() {
      // this.$socket.emit("login", this.name);
    },
    loginSuccess: function(data) {
      // this.name = data;
      this.show = false;
      this.isLogin = true;
      Toast({
        message: "登录成功！",
        duration: 1000
      });
      this.goBottom();
      // this.$refs.myinput.focus()
      // console.log('登录成功');
    },
    loginError: function() {
      this.$refs.login.focus();

      Toast({
        message: "登录失败,用户名已存在！",
        duration: 1000
      });
    },
    sendSuccess: function(data) {
      // this.$refs.myinput.focus()
      this.text = "";
      this.emojiBox = false;
      this.addEle(data);
      setTimeout(() => {
        this.autoGoBottom();
      }, 50);
      // this.datas.push(data);
    },
    sendError: function() {
      Toast({
        message: "发送失败,请刷新页面！",
        duration: 1000
      });
    }
  },
  methods: {
    // 滚动通知栏关闭时归还占用的空间
    closeNotice(){
      document.querySelector('.content').style.paddingTop = '45px'
    },
    // 点击其他地方 自动关闭表情输入框
    closeEmoji() {
      this.emojiBox = false;
    },
    //回车发送消息 关闭键盘
    enterSendMessage(e) {
      e.target.blur();
      this.sendMessage();
      // this.show = true
      // setTimeout(() => {
      // this.$refs.login.focus()
      //   Toast('wwwww')
      // }, 100);
    },
    // 获取用户点击的表情 并添加到输入框中
    getEmoji(d) {
      this.text += d.target.innerHTML;
    },
    //  showEmoji() 显示与隐藏表情框
    showEmoji() {
      this.emojiBox = !this.emojiBox;
    },
    // goBottom() 修复微信内置浏览器 固定定位异常
    goBottom() {
      // window.scrollTo(0,0)
      setTimeout(function() {
        var scrollHeight =
          document.documentElement.scrollTop || document.body.scrollTop || 0;
        window.scrollTo(0, Math.max(scrollHeight - 1, 0));
      }, 50);
    },
    // autoGoBottom() 有新消息 自动滚动到最底部
    autoGoBottom() {
      document.querySelector("#box").scrollTop =
        document.querySelector("#box").scrollHeight -
        document.querySelector("#box").clientHeight;
    },
    // join() 输入昵称 加入房间
    join() {
      //  console.log(this.$socket);
      // this.$socket.connect()
      this.name = this.name.trim();
      if (this.name.length > 1 && this.name.length < 9) {
        this.name = this.name.replace(/</g, "&lt;");
        this.name = this.name.replace(/>/g, "&gt;");
        this.$socket.emit("login", this.name);
      } else {
        Toast({
          message: "昵称长度为2~8个字符！",
          duration: 1000
        });
        // this.$refs.login.focus()
      }
    },
    // sendMessage() 发送消息
    sendMessage() {
      if (this.isLogin) {
        this.text = this.text.trim();
        if (this.text.length > 0) {
          this.text = this.text.replace(/</g, "&lt;");
          this.text = this.text.replace(/>/g, "&gt;");
          this.$socket.emit("sendMessage", {
            name: this.name,
            message: this.text
          });
        } else {
          Toast({
            message: "内容不能为空！",
            duration: 800
          });
        }
      } else {
        this.show = true;
      }
    },
    // addEle(data) 添加新消息
    addEle(data) {
      var ele = ``;
      if (this.name == data.name) {
        ele = `
    <div class="main-right clearfix"><img class="img-right" src="${require("../assets/img/02.jpg")}" alt="">
            <div class="qipao-right">
                <p class="nicheng-right ">${data.name}</p>
                <p class='detail'>
                ${data.message}
                </p>
                <svg class="icon i-right" aria-hidden="true">
                    <use xlink:href="#icon-qishuiMBE"></use>
                </svg>
                <svg class="icon i-right2" aria-hidden="true">
                    <use xlink:href="#icon-xigua"></use>
                </svg>
                <span class="p-right"></span>
            </div>
        </div>
        `;
        this.datas.push(ele);
      } else {
        ele = `
    <div class="main-left clearfix">
            <img class="img-left" src="${require("../assets/img/01.jpg")}" alt="">
            <div class="qipao-left">
                <p class="nicheng-left ">${data.name}</p>
                <p class='detail'>   
                ${data.message}
                </p>
                <svg class="icon i-left" aria-hidden="true">
                    <use xlink:href="#icon-fengzheng1"></use>
                </svg>
                <svg class="icon i-left2" aria-hidden="true">
                    <use xlink:href="#icon-icon-test1"></use>
                </svg>
                <span class="p-left"></span>
            </div>
        </div>
    `;
        this.datas.push(ele);
      }
    }
  }
};
</script>
<style lang="less" scoped>
.content {
  height: 100%;
  padding: 70px 0 60px 0;
  box-sizing: border-box;
}
.title {
  position: fixed;
  top: 0;
  left: 0;
  background-color: #fafafa;
  width: 100%;
  .titleBox {
      display: flex;
  justify-content: space-between;
  border-bottom: 0.1px solid #d6d6d6;
  height: 40px;
  line-height: 45px;
  font-size: 14px;
  .arrow-left {
    font-size: 20px;
    transform: translate(8px, 12px);
  }
  .ellipsis {
    font-size: 20px;
    transform: translate(-12px, 12px);
  }
  }
}
.sendBox {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 0px 10px 0 10px;
  border-top: 0.1px solid #d6d6d6;
  box-sizing: border-box;
  background-color: #fafafa;
  .inputbox {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    input {
      caret-color: #999 !important;
      box-sizing: border-box;
      font-size: 14px;
      border: 1px solid #d6d6d6;
      padding: 3px 1em;
      line-height: 35px;
      // margin-left: 10px;
      height: 35px;
      width: 70%;
    }
    input::placeholder {
      font-size: 14px;
      color: #888;
    }
    .biaoqing {
      font-size: 30px;
      color: #ffc83d;
    }
    .van-button {
      /deep/ span {
        font-size: 14px;
      }
    }
  }

  .emojiBox {
    display: flex;
    flex-wrap: wrap;
    // padding: 0 10px;
    text-align: center;
    // justify-content: space-around;
    width: 100%;
    height: 180px;
    overflow: auto;
    // background-color: yellow;
    padding: 5px 0;
    // margin-top: 15px;
    box-sizing: border-box;
    span {
      display: inline-block;
      width: 12.5%;
      font-size: 30px;
    }
  }
}
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.block {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  // justify-content: center;
  width: 80%;
  height: 30%;
  max-height: 30%;
  background-color: #fff;
  border-radius: 10px;
  .close {
    position: absolute;
    top: 0px;
    right: 0px;
    font-size: 18px;
    display: inline-block;
    padding: 10px;
    // background-color: yellow;
  }
  .ic-left {
    position: absolute;
    left: 29.5%;
    top: 9%;
  }
  .ic-right {
    position: absolute;
    right: 30%;
    top: 11%;
  }
  h3 {
    position: absolute;
    top: 8%;
    left: 50%;
    transform: translateX(-50%);
    color: skyblue;
    font-family: "方正舒体";
    font-size: 25px;
  }
  input {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 60%;
    height: 30px;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    border: 2px solid skyblue;
    box-sizing: border-box;
    padding: 0 0 0 13px;
    caret-color: #f27596;
    font-family: "黑体";
  }
  input::placeholder {
    color: #f27596;
    // text-align: center;
  }
  .start {
    position: absolute;
    left: 50%;
    bottom: 8%;
    transform: translateX(-50%);
    font-family: "黑体";
    width: 60%;
    font-size: 16px;
  }
}
.van-notice-bar {
  height: 25px;
  /deep/ .van-notice-bar__right-icon {
    transform: translate(5px, 0px);
  }
  /deep/ .van-notice-bar__left-icon {
    transform: translate(-4px, 0px);
  }
}
</style>
